package com.ruanjian2001.controller;

import com.ruanjian2001.Service.UserService;
import com.ruanjian2001.entity.User;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 登录页面
 */
public class LoginServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         String username = request.getParameter("username");
         String password = request.getParameter("password");
         UserService us = new UserService();
         User user =  us.login(username,password);

         //如果数据库中有该对象，则把该对象放到session中
          if(user != null){
              request.getSession().setAttribute("user",user);
              response.sendRedirect(request.getContextPath() + "/client/myAccount.jsp");
              System.out.println("登录成功");
          }else{
              request.getRequestDispatcher("/client/login.jsp").forward(request, response);
          }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
